#include "log_xml_format.h"
#include <sstream>
#include <log_utils.h>
#include <vector>
#include <ostream>
using namespace std;
class XMLLabel;

class XMLLabel
{
    using XMLLabels = vector<XMLLabel>;

    friend std::ostream &operator<<(std::ostream &__out, XMLLabel xml);

private:
    string label_{""};
    string content_{""};
    XMLLabels subs_;

public:
    XMLLabel &AddSub(XMLLabel xml)
    {
        subs_.push_back(xml);
        return *this;
    }

    XMLLabel(const string &label, const string &content) : label_(label), content_(content) {}
    XMLLabel(const string &label, const int content) : label_(label), content_(std::to_string(content)) {}
    XMLLabel(const string &label, XMLLabels &subs) : label_(label), subs_(subs) {}
    ~XMLLabel() {}
};

std::ostream &operator<<(std::ostream &__out, XMLLabel xml)
{
    // TODO: 在此处插入 return 语句
    __out << "<" << xml.label_ << ">";
    __out << xml.content_;

    for (auto &&i : xml.subs_)
    {
        __out << i;
    }

    __out << "</" << xml.label_ << ">";

    return __out;
}

std::string LogXMLFormat::Format(const std::string &level, const std::string &log, const std::string &file, int line)
{
    stringstream ss;

    XMLLabel xml("log", "");
    
    xml.AddSub({"time", GetNow()})
        .AddSub({"level", level})
        .AddSub({"text", log})
        .AddSub({"file", file})
        .AddSub({"line", line});

    ss
        << xml;

    return ss.str();
}
